//
//  GroupCodeViewController.swift
//  placepic
//
//  Created by elesahich on 2020/09/05.
//  Copyright © 2020 elesahich. All rights reserved.
//

import UIKit

class RegisterGroupViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    var constraints: [NSLayoutConstraint] = []
    
    let statusBar: UIView = {
        let view = UIView()
        view.frame = CGRect.zero
        view.backgroundColor = UIColor.blueGray50
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupTableViewController()
        setupNavigationBar()
        makeInitialProgressBar()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        animateStatusBar()
    }
}
extension RegisterGroupViewController {
    
    func makeInitialProgressBar() {
        self.view.addSubview(statusBar)
        
        constraints = [
            statusBar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant:  0),
            statusBar.bottomAnchor.constraint(equalTo: tableView.topAnchor, constant: 0),
            statusBar.heightAnchor.constraint(equalToConstant: 1.5),
            statusBar.widthAnchor.constraint(equalToConstant: self.view.frame.width / 3 - 5)
        ]
        NSLayoutConstraint.activate(constraints)
    }
    
    func animateStatusBar() {
        NSLayoutConstraint.deactivate(constraints)
        
        constraints = [
            statusBar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant:  0),
            statusBar.bottomAnchor.constraint(equalTo: tableView.topAnchor, constant: 0),
            statusBar.heightAnchor.constraint(equalToConstant: 1.5),
            statusBar.widthAnchor.constraint(equalToConstant: (view.frame.width/4)*2 + 5 )
        ]
        NSLayoutConstraint.activate(constraints)
        
        UIView.animate(withDuration: 1.5,
                       delay: 0.3,
                       usingSpringWithDamping: 1,
                       initialSpringVelocity: 1,
                       options: .curveEaseOut,
                       animations: {
                        self.view.layoutIfNeeded()
        }, completion: nil)
    }
    
    private func setupNavigationBar() {
        guard let navigationBar = self.navigationController?.navigationBar else { return }
        
        navigationBar.isTranslucent = true
        navigationBar.backgroundColor = UIColor.clear
        navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navigationBar.shadowImage = UIImage()
        
        let leftTitle: UILabel = {
            let label = UILabel()
            label.text = "그룹 참여하기"
            label.font = .boldSystemFont(ofSize: 18)
            label.textColor = UIColor.gray90
            return label
        }()
        
        let backButton: UIButton = {
            let bt = UIButton()
            bt.setImage(UIImage(named: "backArrowIc"), for: .normal)
            return bt
        }()
        
        backButton.addTarget(self,
                             action: #selector(dismissVC),
                             for: .touchUpInside)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftTitle)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: backButton)
    }
    
    @objc
    private func dismissVC() {
        navigationController?.popViewController(animated: true)
    }
}

extension RegisterGroupViewController: UITableViewDelegate { }
extension RegisterGroupViewController: UITableViewDataSource {
    
    private func setupTableViewController() {
        tableView.delegate = self
        tableView.dataSource = self
        let nib = UINib.init(nibName: GroupListTableViewCell.nibName, bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: GroupListTableViewCell.reuseIdentifier)
        tableView.separatorStyle = .none
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: GroupListTableViewCell.reuseIdentifier, for: indexPath) as? GroupListTableViewCell else {
            return UITableViewCell()
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.row == 0 {
            guard let vc = storyboard?.instantiateViewController(identifier: "GroupCodeViewController") as? GroupCodeViewController else {
                fatalError()
            }
            navigationController?.pushViewController(vc, animated: true)
        } else {
            guard let vc = storyboard?.instantiateViewController(identifier: "LoginSuccessViewController") as? LoginSuccessViewController else {
                fatalError()
            }
            navigationController?.pushViewController(vc, animated: false)
        }
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 76
    }
}
